<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="商品分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <select-category :select-flag="true" v-model="model.categoryId"
                               :placeholder="'请选择商品分类'"></select-category>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="组名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attrGroupName">
              <a-input v-model="model.attrGroupName" placeholder="请输入组名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.description" placeholder="请输入描述"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction } from '@/api/manage'
import selectCategory from '../../../components/compents/SelectCategory.vue'
import { getAction, postAction } from '../../../api/manage'

export default {
  name: 'PmsAttrGroupForm',
  components: { selectCategory },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  watch: {},
  data() {
    return {
      model: {
        categoryId: '',
        attrGroupName: '',
        description: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        categoryId: [{ required: true, trigger: 'change', message: '请选择商品分类!' }],
        attrGroupName: [{
          required: true,
          trigger: 'change',
          validator: this.verifyAttrGroupName
        }],
        description: [{ required: true, trigger: 'change', message: '请输入属性分组描述!' }]
      },
      url: {
        add: '/product/attrGroup/add',
        edit: '/product/attrGroup/edit',
        queryById: '/product/attrGroup/queryById',
        verifyAttrGroupName: '/product/attrGroup/verifyAttrGroupName'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.visible = true
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    /**
     * 校验分类名称是否重复
     */
    verifyAttrGroupName(rule, value, callback) {
      if (!this.model.categoryId) {
        return callback(new Error('请先选择所属商品分类!'))
      }
      if (value === undefined || value === '') {
        return callback(new Error('请输入属性分组名称!'))
      }
      getAction(this.url.verifyAttrGroupName, {
        id: this.model.id,
        categoryId: this.model.categoryId,
        attrGroupName: value
      }).then(res => {
        if (res.success) {
          callback()
        } else {
          return callback(new Error(res.message))
        }
      })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>